package com.template.domain.entity;

import com.mybatisflex.annotation.Id;
import com.mybatisflex.annotation.Table;
import com.template.common.validgroup.Create;
import com.template.common.validgroup.Menu;
import com.template.common.validgroup.Update;
import com.template.domain.entity.base.BaseEntity;
import com.template.config.mybatisflex.listener.MyInsertListener;
import com.template.config.mybatisflex.listener.MyUpdateListener;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;


/**
 * 实体类。
 *
 * @author dong
 * @since 2023-09-03
 */
@Getter
@Setter
@Accessors(chain = true)
@Table(value = "sys_menu", onInsert = MyInsertListener.class, onUpdate = MyUpdateListener.class)
public class SysMenu extends BaseEntity {

    @Id
    @NotNull(message = "菜单id为必要参数", groups = Update.class)
    private Long id;

    private Long parentId;

    @NotBlank(message = "菜单名称不能为空", groups = {Create.class, Update.class})
    private String name;

    @NotBlank(message = "路由名称不能为空", groups = {Menu.Catalog.class, Menu.Page.class})
    private String routeName;

    @NotBlank(message = "路由路径不能为空", groups = Menu.Page.class)
    private String routePath;

    private String perms;

    @NotBlank(message = "组件名称不能为空", groups = Menu.Page.class)
    private String component;

    @NotNull(message = "菜单类型不能为空")
    private Integer type; // 0:目录 1:页面 2:按钮

    private String icon;

    private Integer order;

    private Boolean newTab;

    private Boolean cache;

    private Boolean hide;

    private Boolean status;

    public static class Type {
        public static final int Catalog = 0;
        public static final int Page = 1;
        public static final int Button = 2;
    }

    /**
     * 自定义排序规则，防止order为null导致运算异常
     * <p>
     * order越小越靠前
     * 若order为null，则排至最后
     * 若order都为null 则按照创建时间升序排序
     */
    public static int compare(SysMenu sysMenu1, SysMenu sysMenu2) {
        if (sysMenu1.getOrder() == null && sysMenu2.getOrder() == null) {
            return sysMenu1.getId() - sysMenu2.getId() > 0 ? 1 : -1;
        } else if (sysMenu1.getOrder() == null) {
            return 1;
        } else if (sysMenu2.getOrder() == null) {
            return -1;
        }
        return sysMenu1.getOrder() - sysMenu2.getOrder();
    }

}
